<?php

class Admin_configController extends Admin_baseController {
    
    public function __construct() {
        parent::__construct();
        $this->assign('menu_id', admin_nodeModel::SYSTEM);
    }
    
    public function editAction() {
        $config_id = Request::getParams('id');
        $data = [];
        $data['name'] = Request::getParams('name');
        $data['ename'] = Request::getParams('ename');
        $data['type'] = intval(Request::getParams('type', 0));
        $data['kvalue'] = Request::getParams('kvalue');
        
        $data['update_time'] = date('Y-m-d H:i:s');

        $config_info = [];
        if ($config_id && !$config_info = self::model('config')->findById($config_id)) {
            Response::showMsg('信息查询失败，请稍后重试');
        }
        if (Request::isGet()) {
            $this->assign('type_list', self::model('config')->getAllType());
            $this->assign('config_info', $config_info);
            $this->display();
            exit;
        }
        if ($config_id) {//修改
            if (self::model('config')->updateById($config_id, $data)) {
                Response::ajax(200, '修改成功');
            }
        } else {//新增
            $data['add_time'] = date('Y-m-d H:i:s');
            if (self::model('config')->insert($data)) {
                Response::ajax(200, '添加成功');
            }
        }
        Response::ajax(NORMAL_ERROR, '数据处理失败，请稍后重试');
    }
    
    public function listAction() {
        $page = Request::getParams('page', 1);
        $page_size = Request::getParams('page_size', 20);
        $where = $value = $url = array();
        
        $total = self::model('config')->count(null, $where, $value);
        $this->assign(array(
            'page' => $page,
            'page_size' => $page_size,
            'total' => $total,
            'type_list' => self::model('config')->getAllType(),
            'list' => self::model('config')->limit(($page - 1) * $page_size, $page_size)->order('id desc')->getAll($where, $value),
            'pageHtml' => self::instance('Pagination')->page($page)->num($page_size)->total($total)->url('/admin_config/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml()
        ));
        $this->display();
    }
    
    public function deleteAction() {
        $config_id = Request::getParams('id');
        if(self::model('config')->delete('id=?', $config_id)) {
            Response::ajax(200, '删除成功');
        }
        Response::ajax(NORMAL_ERROR, '删除失败');
    }
}